package com.usoft;

import akka.dispatch.ExecutionContexts;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;
import akka.dispatch.OnSuccess;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;

import java.util.concurrent.Executors;

/**
 * Created by liyanxin on 2015/1/9.
 */
public class FutureAddThen {

    public static void main(String args[]) {
        ExecutionContextExecutorService ec = ExecutionContexts.
                fromExecutorService(Executors.newCachedThreadPool());

        Future<String> future1 = Futures.successful("value").andThen(
                new OnComplete<String>() {
                    public void onComplete(Throwable failure, String result) {
                        if (failure != null)
                            sendToIssueTracker(failure);
                    }
                }, ec).andThen(new OnComplete<String>() {
            public void onComplete(Throwable failure, String result) {
                if (result != null)
                    sendToTheInternetz(result);
            }
        }, ec);

        future1.onSuccess(new OnSuccess<String>() {
            @Override
            public void onSuccess(String result) throws Throwable {
                System.out.println(result);
            }
        }, ec);

    }

    private static void sendToTheInternetz(String result) {
        System.out.println(result);
    }

    private static void sendToIssueTracker(Throwable failure) {
        failure.printStackTrace();
    }
}
